package bean;

import java.io.Serializable;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import applicationutil.ApplicationConstant;
import applicationutil.ApplicationUtil;
import dao.V100Dao;

@ManagedBean(name = "loginBean")
@ViewScoped
public class LoginBean implements Serializable {
	private String userName;
	private String passWord;
	private String message;

	public LoginBean() {
		try {
			if (ApplicationUtil.getSession(ApplicationConstant.ADMIN_NAME) != null)
				ApplicationUtil.navigateTo("manage_index.xhtml");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public void checkLogin() {
		try {
			V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
			String s = v100Dao.checkLogin(userName, ApplicationUtil.convertToMD5(passWord));
			if (s != null) {
				ApplicationUtil.setSession(ApplicationConstant.ADMIN_NAME, s);
				java.util.Date date=new java.util.Date();
				DateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy");
				ApplicationUtil.navigateTo("manage_index.xhtml");
			} else
			{
				System.out.println("---dang nhap k thanh cong");
				message="Đăng nhập không thành công!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
